using System;
using System.Collections.Generic;

namespace Nairo.Parser.Dom
{
	public class UniversalSelector : AbstractCSSElement
	{
		public UniversalSelector(FileUnit file)
			: base("*", new List<CSSAttribute>(), file)
		{
		}
		
		public override CSSAttribute GetAttribute(string name)
		{
			foreach (CSSAttribute attribute in this.attributes) {
				if (attribute.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
					return attribute;
				}
			}
			
			return null;
		}
		
		public override void SetAttribute(string name, object value)
		{
			foreach (CSSAttribute attribute in this.attributes) {
				if (attribute.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
					if (value.GetType().Equals(attribute.Type)) {
						attribute.Value = value;
						return;
					} else {
						throw new ArgumentException("Wrong type for value! '" + attribute.Type.Name + "' needed!");
					}
				}
			}
			
			this.attributes.Add(new CSSAttribute(name, value));
		}
	}
	
	public class IDSelector : AbstractCSSElement
	{
		string id, tag;
		
		public IDSelector(string id, string tag, FileUnit file)
			: base(tag + "#" + id, new List<CSSAttribute>(), file)
		{
			this.tag = tag;
			this.id = id;
		}
		
		public override CSSAttribute GetAttribute(string name)
		{
			foreach (CSSAttribute attribute in this.attributes) {
				if (attribute.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
					return attribute;
				}
			}
			
			return null;
		}
		
		public override void SetAttribute(string name, object value)
		{
			foreach (CSSAttribute attribute in this.attributes) {
				if (attribute.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
					if (value.GetType().Equals(attribute.Type)) {
						attribute.Value = value;
						return;
					} else {
						throw new ArgumentException("Wrong type for value! '" + attribute.Type.Name + "' needed!");
					}
				}
			}
			
			this.attributes.Add(new CSSAttribute(name, value));
		}
	}
	
	public class TypeSelector : AbstractCSSElement
	{
		public TypeSelector(string selector, FileUnit file)
			: base(selector, new List<CSSAttribute>(), file)
		{
		}
		
		public override CSSAttribute GetAttribute(string name)
		{
			foreach (CSSAttribute attribute in this.attributes) {
				if (attribute.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
					return attribute;
				}
			}
			
			return null;
		}
		
		public override void SetAttribute(string name, object value)
		{
			foreach (CSSAttribute attribute in this.attributes) {
				if (attribute.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase)) {
					if (value.GetType().Equals(attribute.Type)) {
						attribute.Value = value;
						return;
					} else {
						throw new ArgumentException("Wrong type for value! '" + attribute.Type.Name + "' needed!");
					}
				}
			}
			
			this.attributes.Add(new CSSAttribute(name, value));
		}
	}
}
